package zhuyun.code.launcheractivity.view.widget

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import zhuyun.code.launcheractivity.R

/**
 * @author: Administrator
 * @date: 2021/11/21
 */
class TypefaceTextView(private val mContext: Context, attrs:AttributeSet?, defStyleAttr:Int): AppCompatTextView(mContext,attrs,defStyleAttr) {
    constructor(mContext: Context,attrs: AttributeSet?):this(mContext,attrs,0)
    constructor(mContext: Context):this(mContext,null)
    private var myTypeface:Int = 0
    init {
        val typeArray = mContext.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView)
        myTypeface = typeArray.getInt(R.styleable.TypefaceTextView_myTypeface,myTypeface)
        typeArray.recycle()
        val assetManager = mContext.assets
        var fromAsset:Typeface? = null
        when(myTypeface){
            0->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.PingFangMedium.path)
            }
            1->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.PingFangSCBold.path)
            }
            2->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.PingFangHeavy.path)
            }
            3->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.ArialBoldMT.path)
            }
            4->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.SFProTextRegular.path)
            }
            5->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.SFProTextBold.path)
            }
            6->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.SFProTextSemibold.path)
            }
            7->{
                fromAsset = Typeface.createFromAsset(assetManager,FONTS.SFProTextMedium.path)
            }
        }
        if(fromAsset != null){
            typeface = fromAsset
        }

    }

    enum class FONTS(val path:String){
        PingFangSCBold("fonts/PingFang_SC_Bold.ttf"),
        PingFangMedium("fonts/PingFang-Medium.ttf"),
        ArialBoldMT("fonts/Arial-BoldMT.ttf"),
        PingFangHeavy("fonts/PingFang Heavy.ttf"),
        SFProTextRegular("fonts/SF Pro Text Regular Regular.ttf"),
        SFProTextBold("fonts/SF-Pro-Text-Bold.ttf"),
        SFProTextSemibold("fonts/SFProText_Semibold.ttf"),
        SFProTextMedium("fonts/SFProText_Medium.ttf")
    }
}